package cn.lyq.card.ui.store

import android.content.Context
import android.content.Intent
import androidx.lifecycle.Observer
import cn.lyq.card.R
import cn.lyq.card.base.BaseActivity
import cn.lyq.card.data.model.StoreInfo
import cn.lyq.card.data.src.Status
import cn.lyq.card.ui.phone.PhoneCallFg
import cn.lyq.card.utils.setClick
import com.blankj.utilcode.util.ToastUtils
import kotlinx.android.synthetic.main.act_agent_detail.tvName
import kotlinx.android.synthetic.main.act_agent_detail.tvOpenTime
import kotlinx.android.synthetic.main.act_agent_detail.tvPhone
import kotlinx.android.synthetic.main.act_store_detail.*


class StoreDetailActivity : BaseActivity() {

    private val mModel by lazy {
        getVM(StoreVM::class.java)
    }
    private var mInfo: StoreInfo? = null

    companion object {

        private const val KEY_INFO = "INFO"

        @JvmStatic
        fun start(context: Context, info: StoreInfo) {
            val starter = Intent(context, StoreDetailActivity::class.java)
                .putExtra(KEY_INFO, info)
            context.startActivity(starter)
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.act_store_detail
    }

    override fun initView() {
        super.initView()
        setMyTitle(R.string.str_store_detail)

        mInfo = intent.getSerializableExtra(KEY_INFO)
            ?.takeIf { it is StoreInfo }
            ?.run { this as StoreInfo }
    }

    override fun initListener() {
        super.initListener()
        tvPhone.setClick {
            val merchantPhone = mInfo?.merchantPhone
            if (merchantPhone?.isEmpty() != false) {
                ToastUtils.showShort(getString(R.string.str_store_phone_empty_tip))
            } else {
                tvPhone.setClick {
                    PhoneCallFg.newInstance(merchantPhone)
                        .show(supportFragmentManager, "PhoneCallFg")
                }
            }
        }
    }

    override fun initObserver() {
        super.initObserver()
        mModel.getStoreDetail(mInfo?.merchantNo ?: "").observe(this, Observer {
            when (it?.status) {
                Status.LOADING -> showLoading()
                Status.SUCCESS -> {
                    mInfo = it.requireData()
                    setData()
                    dismissLoading()
                }
                Status.ERROR -> {
                    ToastUtils.showShort(it.message.getMsg())
                    setData()
                    dismissLoading()
                }
            }
        })
    }

    private fun setData() {
        mInfo?.let {
            tvName.text = it.merchantName ?: "商户"
            tvPhone.text = it.merchantPhone ?: getString(R.string.str_store_phone_empty_tip)
            tvPhone.isEnabled = (it.merchantPhone ?: "").isNotEmpty()
            tvOpenTime.text = it.addTime ?: ""
            tvDeviceType.text = it.title ?: it.posName ?: ""
            tvDeviceSn.text = it.posSn ?: ""
            tvActiveTime.text = it.actTime ?: ""
        }
    }

}